package com.easyedu.dean.service;

import com.easyedu.core.entity.DbError;
import com.easyedu.core.entity.po.Teacher;
import com.easyedu.core.exception.EasyEduException;
import com.easyedu.core.dao.TeacherDao;
import com.easyedu.dean.entity.dto.request.ReqTeacher;
import com.easyedu.core.entity.query.TeacherQuery;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author lmr
 **/
@Service
public class TeacherService {

     private static final String STR_FORMAT_3 = "000";

     private final TeacherDao teacherDao;

     public TeacherService(TeacherDao teacherDao) {

          this.teacherDao = teacherDao;
     }

     public List<Teacher> getTeacherList(TeacherQuery teacherQuery) {

          return teacherDao.getTeacher(teacherQuery);
     }

     public String createTeachers(List<ReqTeacher> reqTeachers) {

          DecimalFormat decimalFormat = new DecimalFormat(STR_FORMAT_3);
          CopyOnWriteArrayList<Teacher> teacherList = new CopyOnWriteArrayList<>();
          reqTeachers.parallelStream().forEach(reqTeacher -> {
               Teacher teacher =
                       new Teacher(0, reqTeacher.getTeacherNumber(), reqTeacher.getName(), reqTeacher.getInstituteId(),
                               reqTeacher.getInstituteName(), reqTeacher.getDepartmentId(),
                               reqTeacher.getDepartmentName(), reqTeacher.getRank(), reqTeacher.getJob(),
                               reqTeacher.getPhone(), reqTeacher.getEmail(), reqTeacher.getProfile(), null, null);
               createTeacher(teacher, decimalFormat, teacherList);
          });
          return "共" + reqTeachers.size() + "位老师需新增，已新增" + teacherList.size() + "位";
     }

     @Transactional(rollbackFor = Exception.class)
     public void createTeacher(Teacher teacher, DecimalFormat decimalFormat,
                               CopyOnWriteArrayList<Teacher> teacherList) {

          teacherDao.createTeacher(teacher);
          String teacherNumber =
                  Calendar.getInstance().get(Calendar.YEAR) + decimalFormat.format(teacher.getDepartmentId())
                          + decimalFormat.format(teacher.getTeacherId());
          teacher.setTeacherNumber(Long.parseLong(teacherNumber));
          teacherDao.updateTeacherNumber(teacher);
          teacherList.add(teacher);
     }

     @Transactional(rollbackFor = Exception.class)
     public void deleteTeacher(Long teacherId) {

          Integer integer = teacherDao.deleteTeacher(teacherId);
          if (integer != 1) {
               throw new EasyEduException(DbError.Err_DELETE_DB_ERROR, "删除教师失败");
          }
     }

     public Teacher getTeacherById(String teacherId) {

          return teacherDao.getTeacherById(teacherId);
     }

     @Transactional(rollbackFor = Exception.class)
     public void updateTeacher(ReqTeacher reqTeacher) {
          Integer integer = teacherDao
                  .updateTeacher(reqTeacher.getTeacherNumber(), reqTeacher.getName(), reqTeacher.getInstituteId(),
                          reqTeacher.getInstituteName(), reqTeacher.getDepartmentId(), reqTeacher.getDepartmentName(),
                          reqTeacher.getRank(), reqTeacher.getJob(), reqTeacher.getPhone(), reqTeacher.getEmail(),
                          reqTeacher.getProfile());
          if (integer != 1) {
               throw new EasyEduException(DbError.Err_UPDATE_DB_ERROR, "更新教师失败");
          }
     }
}
